<template>
    <div ref="chartBarRef" class="chart-box bg-white pd-16">
        <div class="chart-box">
            <div class="flex flex-jc-sb">
                <div class="fs-14 bold">{{ title }}</div>
                <slot>
                    <div class="flex fs-12">
                        <div class="flex">
                            <div class="icon blue"></div>
                            <div class="mg-l-5">加保人数</div>
                        </div>
                        <div class="flex mg-l-10">
                            <div class="icon light-blue"></div>
                            <div class="mg-l-5">减保人数</div>
                        </div>
                    </div>
                </slot>
            </div>
            <div class="chart-container" ref="chartRef">
            </div>
        </div>
    </div>
</template>
<script setup>
import { ref, watch, onMounted,onActivated } from 'vue'
import * as echarts from 'echarts'
import { getSevenDaysData } from '@/api/clients/signClient.ts'
import { getAddAndReduceTrend } from '@/api/dashboard/longTerm.ts'
const props = defineProps({
    title: {
        type: String,
        default: '近七日加减保趋势'
    },
    companyId: {
        type: [Number, String]
    },
    insureType: {
        type: String,
        default: 'long'
    },
    queryData: {
        type: Object,
        default() {
            return {}
        }
    }
})

const getData = () => {
    if(props.insureType === 'long') {
        getSevenDaysData({
            companyId: props.companyId,
            ...props.queryData
        }).then(res => {
            setOption(res.date, res.add, res.reduce)
        })
    } else if(props.insureType === 'dashboardLong') {
        getAddAndReduceTrend({
            ...props.queryData
        }).then(res => {
            setOption(res.date, res.add, res.reduce)
        })
    }
}

const chartRef = ref(null)
const chartBarRef = ref(null)
let myChart = null

const createOption = (date, add, reduce) => {
    const chartOption =
    {
        tooltip: {
        },
        xAxis: {
            type: 'category',
            data: date
        },
        yAxis: {
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            },
        },
        grid: {
            top: 40,
            bottom: 40,
            left: 40,
            right: 40,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow',
                }
            }
        },
        series: [{
            name: '加保人数',
            type: 'bar',
            data: add,
            seriesLayoutBy: 'row',
            color:
                new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                        offset: 0,
                        color: '#1A64FD'
                    },
                    {
                        offset: 1,
                        color: '#657EFB'
                    }
                ])
        }, {
            name: '减保人数',
            type: 'bar',
            data: reduce,
            seriesLayoutBy: 'row',
            color:
                new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                        offset: 0,
                        color: '#EBEFFE'
                    },
                    {
                        offset: 1,
                        color: '#EBEFFE'
                    }
                ])
        }]
    }
    return chartOption
}

const setOption = (date, add, reduce) => {
    myChart.setOption(createOption(date, add, reduce))
}

const resize = () => {
    myChart.resize()
}

defineExpose({
    chartBarRef,
    resize
})

watch(() => props, (newval) => {
    if (newval.insureType === 'long') {
        if (newval.companyId) {
            getData()
        }
    } else {
        getData()
    }
}, {
    // immediate: true,
    deep: true
})

onMounted(() => {
    myChart = echarts.init(chartRef.value)
    // setOption()
})
onActivated(() => {
    myChart = echarts.init(chartRef.value)
    // setOption()
})
</script>
<style lang="scss" scoped>
.chart-box {
    box-sizing: border-box;
}

.chart-box,
.chart-container {
    width: 100%;
    height: 100%;
}

.icon {
    width: 12px;
    height: 12px;
    border-radius: 2px;
}

.blue {
    background: #2567FC;
}

.light-blue {
    background: #EBEFFE;
}
</style>